from NewsMining.db.dbConnect import getDBConnection
from mynewsweb.model import *
import sys

#==== ==== ==== ====
# CHOSE BOW
#==== ==== ==== ====
db = getDBConnection()

def getBow(count , limit):
    # Besede, ki se najveckrat pojavijo v bazi
    most_freq_words = db.execute("""   select word_id, sum(count) as s
                                       from   news_words
                                       group by word_id
                                       order by sum(count) desc
                                       limit %s""" % (limit)).fetchall()
    #having sum(count) > %s
    # Besede, ki so v najmanj dokumentih
    low_documents_words = db.execute("""   select word_id, count(*) as c
                                           from   news_words
                                           group by word_id
                                           having count(*) > %s
                                           order by count(*) asc
                                           limit %s""" % (count, limit)).fetchall()

    most_freq_words = [w for w, s in most_freq_words]
    return dict([(w, c) for w, c in low_documents_words if w in most_freq_words])

    # Presek
    #words = set([w for w, s in most_freq_words]).intersection([w for w, c in low_documents_words])


def getLimit(count, wordsLimit):
    words = 0
    limit = 1000
    r = 0
    while words == 0 or words < wordsLimit and limit < 10000:
        limit += 700
        words = len(getBow(count, limit))
        if words < wordsLimit:
            r = limit

    return r

def main():
    print "Stevilo vseh besed:", db.query(Word).count()

    try:
        count = int(sys.argv[1])
        limit = int(sys.argv[2])
    except:
        print "WordSeqKernelOrange.py numTests sample_length count limit"
        count = 5
        limit = 5000

    print "limit: %s, count: %s" % (count, limit)
    print "Presek", len(getBow(count, limit))

##    for n in range(6000, 7000, 1100):
##        count, suma = 3, 15
##        print "N: %s, count: %s, suma: %s" % (count, suma, n)
##        print "Presek", len(getBow(count, n))

if __name__ == '__main__':
    main()